package com.ydt.dockingclients;

import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.ydt.dockingclients.api.YdtSdkApi;
import com.ydt.dockingclients.config.SdkYdtConfig;
import com.ydt.dockingclients.config.YdtTokenManage;
import com.ydt.dockingclients.request.SdkYdtOrderQrcodeRequest;
import com.ydt.dockingclients.request.SdkYdtOrderRequest;
import com.ydt.dockingclients.response.*;
import com.ydt.dockingclients.utils.YdtClient;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class YdtApiTestMain {

    /**
     * 获取主卡余额
     *
     * @return
     * @throws Exception
     */
    public YdtMaincardDataResponse.DataDTO maincardDetail() throws Exception {
        YdtMaincardDataResponse response = YdtClient.openApi(YdtSdkApi.V1_MAINCARD_DETAIL, null, YdtTokenManage.getToken(), YdtMaincardDataResponse.class);
        log.info("response:{}", JSON.toJSONString(response));
        if (response.getCode() == HttpStatus.HTTP_OK) {
            return response.getData();
        }
        return null;
    }

    /**
     * 创建订单
     *
     * @param order
     * @throws Exception
     */
    public YdtCreateOrderDataResponse createOrder(SdkYdtOrderRequest order) throws Exception {
        CommonDataResponse response = YdtClient.openApi(YdtSdkApi.V1_ORDER_CREATE, com.alibaba.fastjson2.JSON.toJSONString(order), YdtTokenManage.getToken(), CommonDataResponse.class);
        YdtCreateOrderDataResponse responseData = new YdtCreateOrderDataResponse();
        if (response.getCode() == HttpStatus.HTTP_OK) {
            responseData.setCode(HttpStatus.HTTP_OK);
            String data = com.alibaba.fastjson2.JSON.toJSONString(response.getData());
            log.info("data:{}", data);
            responseData.setData(com.alibaba.fastjson2.JSON.parseObject(data, YdtCreateOrderDataResponse.DataDTO.class));
        } else {
            responseData.setCode(HttpStatus.HTTP_INTERNAL_ERROR);
            responseData.setCode(response.getCode());
            responseData.setMessage(response.getMessage());
        }
        return responseData;
    }

    /**
     * 获取二维吗
     *
     * @param qrcodeParam
     * @throws Exception
     */
    public void qrcode(SdkYdtOrderQrcodeRequest qrcodeParam) throws Exception {
        YdtClient.openApi(YdtSdkApi.V1_ORDER_QRCODE, com.alibaba.fastjson2.JSON.toJSONString(qrcodeParam), YdtTokenManage.getToken(), SdkQrcodeOrderResponse.class);

    }

    /**
     * 查询订单
     *
     * @param customerOrderNo 客户订单编号
     * @throws Exception
     */
    public void detail(String customerOrderNo) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("customerOrderNo", customerOrderNo);
        YdtClient.openApi(YdtSdkApi.V1_ORDER_DETAIL, com.alibaba.fastjson2.JSON.toJSONString(data), YdtTokenManage.getToken(), YdtDetailOrderDataResponse.class);
    }

    /**
     * 退单  客单号&订单号 二选一必填
     *
     * @param customerOrderNo 客单号
     * @param orderNo         订单号
     * @param refundRemark    退款原因
     * @throws Exception
     */
    public YdtRefundOrderDataResponse refund(String customerOrderNo, String orderNo, String refundRemark) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("customerOrderNo", customerOrderNo);
        data.put("orderNo", orderNo);
        data.put("refundRemark", refundRemark);
        return YdtClient.openApi(YdtSdkApi.V1_ORDER_REFUND, com.alibaba.fastjson2.JSON.toJSONString(data), YdtTokenManage.getToken(), YdtRefundOrderDataResponse.class);
    }

    /**
     * 获取油站列表
     *
     * @param pageNo
     * @param pageSize
     * @throws Exception
     */
    public YdtStationListDataResponse stationList(Integer pageNo, Integer pageSize) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("pageNo", pageNo);
        data.put("pageSize", pageSize);
        return YdtClient.openApi(YdtSdkApi.V1_STATION_LIST, com.alibaba.fastjson2.JSON.toJSONString(data), YdtTokenManage.getToken(), YdtStationListDataResponse.class);
    }


    /**
     * 获取单个油站信息
     *
     * @param stationId
     * @throws Exception
     */
    public YdtStationDetailDataResponse stationDetail(long stationId) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("stationId", stationId);
        return YdtClient.openApi(YdtSdkApi.V1_STATION_DETAIL, com.alibaba.fastjson2.JSON.toJSONString(data), YdtTokenManage.getToken(), YdtStationDetailDataResponse.class);
    }

    public static void main(String[] args) throws Exception {
        SdkYdtConfig.appId="";
        SdkYdtConfig.appSecret="";
        YdtStationListDataResponse _db = new YdtApiTestMain().stationList(1, 20);
        log.info("{}", JSON.toJSONString(_db));
    }
}
